home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / NNUTL101.ZIP / NNEVOLVE / NNEVOLVE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  3.6 KB  |  97 lines

  1. /*--------------------------------------------------------------------------*
  2.  * Gregory Stevens                                                   7/1/93 *
  3.  *                              NNEVOLVE.C                                  *
  4.  *                                                                          *
  5.  *                                                                          *
  6.  *--------------------------------------------------------------------------*/
  7. #include "nnbkprop.c"                /* to chain it to the nn*.c utilities  */
  8.  
  9. #define NUM_ITS 1000                 /* iterations before it stops training */
  10.  
  11. void main()
  12. {
  13.   int Patr;
  14.   int Pattern;                          /* for looping through patterns   */
  15.   int Layer;                            /* for looping through layers     */
  16.   int LCV;                              /* for looping training sets      */
  17.   FILE *outputfile, *HidActFile;        /* for saving activations         */
  18.   NNETtype Net;                         /* for the network itself         */
  19.   PATTERNtype InPatterns, OutPattern;   /* for the training patterns      */
  20.  
  21.   HidActFile = fopen( "hidacts.out", "wt" );
  22.   outputfile = fopen( "outnodes.out", "wt" );
  23.  
  24.   Net = InitNet( NUMNODES );           /* initializes the network        */
  25.   InPatterns = InitInPatterns(0);       /* loads input patterns from file */
  26.   OutPattern = InitOutPatterns();       /* loads output patterns from file*/
  27.  
  28.   printf("\n\n\n\n\n");                 /* gets screen ready for output   */
  29.  
  30.   printf( "BEGINNING TRAINING:\n\n" );
  31.  
  32.   for (LCV=0; (LCV < NUM_ITS); ++LCV)   /* loop through a training set    */
  33.     {
  34.       for (Pattern=0; (Pattern<10); ++Pattern)  /* each pattern */
  35.          {
  36.             /* FORWARD PROPAGATION */
  37.             Patr = (rand() % 16);           /* chose a pattern btw. 0, 15 */
  38.  
  39.             Net = UpDateInputAct( InPatterns, Patr, Net );
  40.             for (Layer=1; (Layer<NUMLAYERS); ++Layer)
  41.               {
  42.                  Net = UpDateLayerAct( Net, Layer );
  43.               }
  44.  
  45.             DisplayLayer( Net, 0, 4 );             /* display input layer */
  46.             printf( "   " );
  47.             DisplayLayer( Net, (NUMLAYERS-1), 4);  /* display output layer*/
  48.             printf( "\n" );                        /* new line            */
  49.  
  50.             /* BACKWARD PROPAGATION */
  51.             Net = UpDateWeightandThresh( Net, OutPattern, Patr );
  52.          }
  53.     }
  54.  
  55.   for (LCV=0; (LCV<16); ++LCV)
  56.     {
  57.       /* FORWARD PROPAGATION */
  58.       Net = UpDateInputAct( InPatterns, LCV, Net );
  59.       for (Layer=1; (Layer<NUMLAYERS); ++Layer)
  60.         {
  61.            Net = UpDateLayerAct( Net, Layer );
  62.         }
  63.  
  64.       for (Layer=0; (Layer<NUMNODES[1]); ++Layer)
  65.         fprintf( HidActFile, "%2.2f ", Net.unit[1][Layer].state );
  66.       fprintf( HidActFile, "\n" );
  67.  
  68.       fprintf( outputfile, "%2.2f ", Net.unit[2][0].state );
  69.       fprintf( outputfile, "%2.2f ", Net.unit[2][1].state );
  70.       fprintf( outputfile, "%2.2f ", Net.unit[2][2].state );
  71.       fprintf( outputfile, "%2.2f \n", Net.unit[2][3].state );
  72.     }
  73.  
  74.   {
  75.    FILE *WeightFile;
  76.    WeightFile = fopen( "weights.out", "wt" );
  77.  
  78.    for (Layer=1; (Layer<=NUMLAYERS); ++Layer)
  79.    {
  80.     for (LCV=0; (LCV<NUMNODES[Layer]); ++LCV)
  81.     {
  82.      for (Pattern=0; (Pattern<NUMNODES[Layer-1]); ++Pattern)
  83.      {
  84.       fprintf( WeightFile, "%4.4f ", Net.unit[Layer][LCV].weights[Pattern] );
  85.      }
  86.      fprintf( WeightFile, "\n");
  87.     }
  88.    fprintf( WeightFile, "\n\n");
  89.    }
  90.  
  91.    fclose( WeightFile );
  92.   }
  93.  
  94.   fclose( HidActFile );
  95.   fclose( outputfile );
  96. }
  97.